package com.ygqh.baby.service.impl;
import java.util.Arrays;
import java.util.List;

import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.stereotype.Service;
import  org.springframework.util.Assert;

import  com.ygqh.baby.ao.DataStatus;
import  com.ygqh.baby.ao.QueryInfo;
import  com.ygqh.baby.ao.ResultSet;
import  com.ygqh.baby.mapper.AdminRoleMapper;
import com.ygqh.baby.mapper.AdminRolePermissionMapper;
import  com.ygqh.baby.po.AdminRole;
import  com.ygqh.baby.po.AdminRoleExample;
import com.ygqh.baby.po.AdminRolePermission;
import com.ygqh.baby.po.AdminRolePermissionExample;
import  com.ygqh.baby.service.AdminRoleService;

@Service
public class AdminRoleServiceImpl implements AdminRoleService{
	
	@Autowired
	private AdminRoleMapper adminRoleMapper;
	
	@Autowired
	private AdminRolePermissionMapper rolePermissionMapper;
	@Override
	public List<AdminRole> find() {
		AdminRoleExample example = new AdminRoleExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminRoleMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		AdminRoleExample example = new AdminRoleExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminRoleMapper.countByExample(example);
	}

	@Override
	public AdminRole findById(Long id) {
		return adminRoleMapper.selectByPrimaryKey(id);
	}


	
	@Override
	public AdminRole findDetailById(Long id) {
		return adminRoleMapper.findDetailById(id);
	}

	

	

	@Override
	public ResultSet<AdminRole> search(QueryInfo queryInfo,String q,Boolean isEnabled) {
		List<AdminRole>  list =  adminRoleMapper.selectSuper(queryInfo, q,isEnabled);
		int count = adminRoleMapper.countSuper(q,isEnabled);
		ResultSet<AdminRole> resultSet = new ResultSet<AdminRole>(count,list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		AdminRoleExample example = new AdminRoleExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		AdminRole record = new AdminRole();
		record.setStatus(DataStatus.Delete);
		return adminRoleMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int update(AdminRole adminRole,Long[] permissionIds) {
		
		//先根据角色删除权限 再添加
		rolePermissionMapper.updatePermissionByRoleId(DataStatus.Delete.name(), adminRole.getId());	
		for(Long pid:permissionIds) {
			AdminRolePermission rolePermission = new AdminRolePermission();
			rolePermission.setPermissionId(pid);
			rolePermission.setRoleId(adminRole.getId());
			rolePermissionMapper.insertSelective(rolePermission);
		}
		return adminRoleMapper.updateByPrimaryKeySelective(adminRole);
	}
	@Override
	public int updateByPrimaryKeySelective(AdminRole adminRole) {
		return adminRoleMapper.updateByPrimaryKeySelective(adminRole);
	}
	
	@Override
	public int save(AdminRole role,Long[] permissionIds) {
		int result = adminRoleMapper.insertSelective(role);
		for(Long pid:permissionIds) {
			AdminRolePermission rolePermission = new AdminRolePermission();
			rolePermission.setPermissionId(pid);
			rolePermission.setRoleId(role.getId());
			rolePermissionMapper.insertSelective(rolePermission);
		}
		return result;
	}

	@Override
	public List<AdminRole> findBySupplierId(Long supplierId) {
		AdminRoleExample example = new AdminRoleExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name()).andSupplierIdEqualTo(supplierId).andIsEnabledEqualTo(true);
		return adminRoleMapper.selectByExample(example);
	}
	
}

